<?php

namespace App\Http\Resources;
use App\Helpers\JsonApiResponseData;
use App\Http\Controllers\Plugin\Models\User\CustomerForm;
use App\Models\Class\ClassGoods;
use App\Models\Map\Mch;
use App\Models\Map\MerchantActivity;
use App\Models\Shop\CustomFormConfig;
use Illuminate\Http\Resources\Json\JsonResource;

class ClassGoodsCollection extends JsonResource
{
    use JsonApiResponseData;
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'id'=>$this->id,
            'commission_rate'=>$this->commission_rate,
            'contents'=>$this->contents,
            'cover'=>$this->cover,
            'expire_at'=>$this->expire_at,
            'goods_detail'=>$this->goods_detail,
            'hot_goods'=>$this->hot_goods,
            'hot_recoment'=>$this->hot_recoment,
            'images'=>$this->images,
            'last_sale_time'=>$this->last_sale_time,
            'market_price'=>$this->market_price,
            'max_person_number'=>$this->max_person_number,
            'mch_id'=>$this->mch_id,
            'mch_service'=>$this->mch_service,
            'purchase_notes'=>$this->purchase_notes,
            'quantity'=>$this->quantity,
            'sale_price'=>$this->sale_price,
            'start_sale_time'=>$this->start_sale_time,
            'subtitle'=>$this->subtitle,
            'title'=>$this->title,
            'instructions'=>$this->instructions,
            'mch_name'=>$this->getMch($this->mch_id),
            'hot'=>ClassGoods::query()
                ->select('id','title','cover')
                ->where('status',1)
                ->whereIn('id',$this->hot_recoment)
                ->get(),
            'shops'=>MerchantActivity::query()
                ->select('id','title','cover')
                ->where('status',1)
                ->whereIn('id',$this->hot_goods)
                ->get(),
            'mch_services'=>$this->getServerData($this->mch_service),
            'submit_model_id'=>$this->submit_model_id,
            'custome_model_value'=>$this->getModelValue($this->submit_model_id),
            'customParameter'=>$this->customParameter,
            'is_vip'=>$this->is_vip,
            'course_id'=>$this->course_id,
            'is_address'=>$this->is_address,
            'is_member'=>$this->is_member,
            'is_ercode'=>$this->is_ercode,
            'template_model'=>$this->getTemplate($request->openid,$this->id,$this->submit_model_id)
        ];
    }
    public function getTemplate($openid,$goods_id,$template_id){
       $data =  CustomerForm::query()
            ->where('openid',$openid)
//            ->where('goods_id',$goods_id)
            ->where('template_id',$template_id)
            ->value('custom_submit_info');
       if($data){
           return json_decode($data);
       }else{
           return null;
       }
    }
    protected function getModelValue($id){
        if($id>0) {
           $value =  CustomFormConfig::query()->where('id',$id)->value('value');
           if($value){
               return $value;
           }else{
               return [];
           }
        }else{
            return [];
        }
    }
    protected function getMch($mch_id){
        return Mch::query()->where('id',$mch_id)->value('title');
    }
    protected function getData ($value){
        return $value?json_decode($value,true):[];
    }
    protected function getServerData ($mch_service){
        if(is_array($mch_service)){
            $data = $mch_service;
        }else{
            $data = $this->getData($mch_service);
        }
        $icon = config('icon');
        $map = [];
        if(count($data)>0){
            foreach ($icon as $item){
                foreach ($data as $option){
                    if($item['key']==$option){
                        $map[] = $item;
                    }
                }
            }
        }
        return $map;
    }
}
